using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class TinhTrangPLDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new TinhTrangPL instance filled with the DataReader's current record data 
        /// </summary>
        private TinhTrangPL GetTinhTrangPLFromReader(IDataReader dr)
        {
            var obj = new TinhTrangPL();
            obj.TinhtrangID = (dr["TinhtrangID"] is DBNull) ? Int32.MinValue : (Int32) dr["TinhtrangID"];
            obj.TenTinhtrang = (dr["TenTinhtrang"] is DBNull) ? String.Empty : (String) dr["TenTinhtrang"];
            obj.TenTinhtrang_en = (dr["TenTinhtrang_en"] is DBNull) ? String.Empty : (String) dr["TenTinhtrang_en"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of TinhTrangPL with the data read from the input DataReader
        /// </summary>
        private List<TinhTrangPL> GetTinhTrangPLCollectionFromReader(IDataReader dr)
        {
            var obj = new List<TinhTrangPL>();
            while (dr.Read())
                obj.Add(GetTinhTrangPLFromReader(dr));
            return obj;
        }

        public int Insert(TinhTrangPL obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhtrangID", SqlDbType.Int).Value = obj.TinhtrangID;
                myCommand.Parameters.Add("@TenTinhtrang", SqlDbType.NVarChar).Value = obj.TenTinhtrang;
                myCommand.Parameters.Add("@TenTinhtrang_en", SqlDbType.NVarChar).Value = obj.TenTinhtrang_en;

                return myCommand.ExecuteNonQuery();
            }
        }

        public bool Update(TinhTrangPL obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhtrangID", SqlDbType.Int).Value = obj.TinhtrangID;
                myCommand.Parameters.Add("@TenTinhtrang", SqlDbType.NVarChar).Value = obj.TenTinhtrang;
                myCommand.Parameters.Add("@TenTinhtrang_en", SqlDbType.NVarChar).Value = obj.TenTinhtrang_en;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 TinhtrangID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhtrangID", SqlDbType.Int).Value = TinhtrangID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_TinhTrangPL with the specified ID
        /// </summary>
        public TinhTrangPL GetTinhTrangPLByID(Int32 TinhtrangID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_GetTinhTrangPLByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhtrangID", SqlDbType.Int).Value = TinhtrangID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetTinhTrangPLFromReader(dr) : null);
            }
        }

        //Get All TinhTrangPL
        public List<TinhTrangPL> GetAllTinhTrangPL(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_GetAllTinhTrangPL", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetTinhTrangPLCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  TinhTrangPL
        public int GetCountTinhTrangPL()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhTrangPL_GetCountTinhTrangPL", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}